<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-content">
        <div class="logo">
          <h2>社区球场预约系统</h2>
        </div>
        <div class="nav-buttons">
          <el-button
            :type="activeTab === 'courts' ? 'primary' : 'default'"
            @click="switchTab('courts')"
            :icon="Location"
          >
            球场列表
          </el-button>
          <el-button
            :type="activeTab === 'bookings' ? 'primary' : 'default'"
            @click="switchTab('bookings')"
            :icon="Calendar"
          >
            预约管理
          </el-button>
          <el-button
            :type="activeTab === 'profile' ? 'primary' : 'default'"
            @click="switchTab('profile')"
            :icon="User"
          >
            个人信息
          </el-button>
        </div>
        <div class="user-info">
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              <el-icon><User /></el-icon>
              {{ userStore.userInfo?.name || '用户' }}
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <!-- 主内容区域 -->
    <el-main class="main-content">
      <router-view />
    </el-main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Location, Calendar, User, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const activeTab = ref('courts')

const switchTab = (tab) => {
  activeTab.value = tab
  switch (tab) {
    case 'courts':
      router.push('/')
      break
    case 'bookings':
      router.push('/bookings')
      break
    case 'profile':
      router.push('/profile')
      break
  }
}

const handleCommand = async (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      activeTab.value = 'profile'
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        userStore.logout()
        ElMessage.success('已退出登录')
        router.push('/login')
      } catch {
        // 用户取消
      }
      break
  }
}

onMounted(() => {
  // 根据当前路由设置活动标签
  const currentPath = router.currentRoute.value.path
  if (currentPath === '/') {
    activeTab.value = 'courts'
  } else if (currentPath === '/bookings') {
    activeTab.value = 'bookings'
  } else if (currentPath === '/profile') {
    activeTab.value = 'profile'
  }
})
</script>

<style scoped>
.layout-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.logo h2 {
  margin: 0;
  color: #409eff;
  font-weight: bold;
}

.nav-buttons {
  display: flex;
  gap: 10px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f7fa;
}

.main-content {
  flex: 1;
  background-color: #f5f7fa;
  padding: 20px;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: 10px;
    padding: 10px;
  }
  
  .nav-buttons {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .main-content {
    padding: 10px;
  }
}
</style>
